package controllers

import dao._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.routing.JavaScriptReverseRouter
import tool.{FormTool, Tool}

import java.io.File
import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import implicits.Implicits._

/**
 * Created by Administrator on 2019/8/7
 */
class ToolController @Inject()(cc: ControllerComponents)(
  implicit val accountDao: AccountDao,
  implicit val userDao: UserDao,
  implicit val memberDao: MemberDao,
  implicit val companyMemberDao: CompanyMemberDao,
  implicit val specialMemberDao: SpecialMemberDao,
) extends
  AbstractController(cc) {

  def viewFile = Action { implicit request =>
    val data = FormTool.filePathForm.bindFromRequest().get
    val file = new File(data.filePath)
    Ok.sendFile(file, onClose = () => {
      file.deleteOnExit()
    })
  }

  def uploadFile = Action(parse.multipartFormData) { implicit request =>
    val tmpDir = Tool.createTempDirectory("tmpDir")
    val file = Tool.fileMoveDir("file", tmpDir)
    Ok(Json.obj("valid" -> true, "filePath" -> file.getAbsolutePath))
  }


}
